package com.xjoyt.pay.share.controllers;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xjoyt.pay.controllers.AbstractController;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.share.interfaces.BucketContract;
import com.xjoyt.pay.share.params.BucketPageParam;
import com.xjoyt.pay.share.po.Bucket;
import com.xjoyt.pay.share.services.BucketService;

@RestController
@RequestMapping("bucket")
public class BucketController extends AbstractController implements BucketContract {

	@Autowired
	private BucketService service;

	@Override
	public PageResult<Bucket> loadPage(@RequestBody PageParam<BucketPageParam> param) {
		if (StringUtils.isEmpty(param.order)) {
			param.order = "desc";
		}
		if (StringUtils.isEmpty(param.sort)) {
			param.sort = "addedDate";
		}
		if (param.query == null)
			param.query = new BucketPageParam();
		return service.loadPage(param);
	}

	@Override
	public ExecuteResult<?> setDefault(@PathVariable("id") String id) {
		return service.setDefault(id);
	}

	@Override
	public Bucket getDefault() {
		return service.getDefault();
	}

	@Override
	public ExecuteResult<Bucket> save(@RequestBody Bucket param) {
		return service.save(param);
	}

	@Override
	public ExecuteResult<?> remove(@PathVariable("id") String id) {
		return service.delete(id);
	}
}
